using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using PowerPoint;

namespace Dare.Utilities.Microsoft
{
    public class PowerPointHelper
    {
        public static bool IsPowerPointInstalled()
        {
            try
            {
                ApplicationClass pptApp = new ApplicationClass();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static FileInfo[] SavePpt2Images(string pptFileName, PowerPointSaveAsSupportImageFormat imageFormat, int width, int height, string saveToPath, bool usePptMd5AsSubPathName)
        {
            if (!System.IO.Directory.Exists(saveToPath))
            {
                System.IO.Directory.CreateDirectory(saveToPath);
            }

            if (usePptMd5AsSubPathName)
            {
                saveToPath = System.IO.Path.Combine(saveToPath, Utilities.IO.StreamHash.ComputeFileMD5(pptFileName).ToString("N"));
                if (!System.IO.Directory.Exists(saveToPath))
                {
                    System.IO.Directory.CreateDirectory(saveToPath);
                }
            }

            using (PowerPointWrapper app = new PowerPointWrapper())
            {
                app.Open(pptFileName);
                List<FileInfo> files = new List<FileInfo>(app.GetSlideCount());
                int count = app.GetSlideCount() + 1;
                saveToPath = System.IO.Path.Combine(saveToPath, "{0}." + imageFormat.ToString().ToLower());

                return app.SaveRangeAsImage(imageFormat, 0, 0, width, height, saveToPath);
            }
        }
    }
}
